//
// Created by Lenovo on 2024/2/29.
//
#include<bits/stdc++.h>
using namespace std;
//被围绕的区域
class Solution {
public:

    int m,n;
    bool st[201][201];
    bool flag= true;        //flag表示当前'O'区域是否被'X'包围
    int go[4][2]={{-1,0},{1,0},{0,-1},{0,1}};


    void dfs(int curX,int curY,vector<vector<char>>& board,vector<pair<int,int>>& list){        //list记录当前区域所有点的坐标
        if(curX==0 || curX==m-1 || curY==0 || curY==n-1){
            flag= false;
        }
        for(int i=0;i<4;i++){
            int nextX=curX+go[i][0];
            int nextY=curY+go[i][1];
            if(nextX>=0 && nextX<m && nextY>=0 && nextY<n && !st[nextX][nextY] && board[nextX][nextY]=='O'){
                st[nextX][nextY]=true;
                list.push_back({nextX,nextY});
                dfs(nextX,nextY,board,list);
            }
        }
    }

    void solve(vector<vector<char>>& board) {
        m=board.size();
        n=board[0].size();
        for(int i=0;i<m;i++){
            for(int j=0;j<n;j++){
                if(!st[i][j] && board[i][j]=='O'){
                    flag=true;
                    st[i][j]=true;
                    vector<pair<int,int>> list;
                    list.push_back({i,j});
                    dfs(i,j,board,list);
                    if(flag){
                        for(int k=0;k<list.size();k++){
                            pair<int,int> point=list[k];
                            board[point.first][point.second]='X';
                        }
                    }
                }
            }
        }
    }
};